package com.study.algorithm.niuke.huawei.testquestionsone;

import java.util.Scanner;

public class MetricConversion {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个十六进制字符串：");
        MetricConversion conversion = new MetricConversion();
//        while (scanner.hasNext()) {
////            String inputStr = scanner.next();
////            conversion.conversion(inputStr.toCharArray());
////        }

        conversion.conversion("1A".toCharArray());
    }

    public void conversion(char[] Ox) {
        int length = Ox.length;
        int intVal = 0;
        for (int i = 0; i < length; i++) {
            int currVal = switchCharToInt(Ox[i]);
            System.out.println("字符：" + Ox[i] + ",转换值：" + currVal);
            if (currVal == -1) {
                System.out.println("输入有误，请重新输入");
                return;
            }
            if (intVal == 0) {
                intVal = currVal;
                continue;
            }
            intVal = intVal * 16 + currVal;
        }

        System.out.println("十六进制字符：" + Ox + ",转化为十进制：" + intVal);
    }

    private int switchCharToInt(char in) {
        if (in >= '0' && in <= '9') {
            return in - '0';
        }

        if (in >= 'A' && in <= 'F') {
            return in - 'A' + 10;
        }
        return -1;
    }

}
